package com.feng.server;

import com.feng.server.config.ServerConfig;
import com.feng.server.handler.ChatHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

public class ChatServer {
    private int port = ServerConfig.getPort();
    private String webSocketPath = ServerConfig.getWebSocketPath();

    public void start() {
        //配置服务端的NIO线程组
        //实际上EventLoopGroup就是Reactor线程组
        //两个Reactor一个用于服务端接收客户端的连接，另一个用于进行SocketChannel的网络读写
        ServerBootstrap serverBootstrap = new ServerBootstrap();

        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workGroup = new NioEventLoopGroup();
        serverBootstrap.group(bossGroup, workGroup)
                .channel(NioServerSocketChannel.class)
                //回调请求
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        String address = socketChannel.remoteAddress().getHostString();

                        ChannelPipeline pipeline = socketChannel.pipeline();
                        pipeline.addLast("httpServerCodec", new HttpServerCodec());
                        pipeline.addLast("httpObjectAggregator", new HttpObjectAggregator(65536));//聚合器，使用websocket会用到
                        pipeline.addLast("chunkedWriteHandler", new ChunkedWriteHandler());//用于大数据的分区传输
                        pipeline.addLast(new WebSocketServerProtocolHandler(webSocketPath));
                        pipeline.addLast("chatHandler", new ChatHandler());

                    }
                })
                //.localAddress(new InetSocketAddress(port))
                //配置NioServerSocketChannel的TCP参数
                .option(ChannelOption.SO_BACKLOG, 1024);
//                .option(ChannelOption.SO_KEEPALIVE, true);
        ChannelFuture channelFuture = null;
        try {
            System.out.println("chat server starting in " + port);
            channelFuture = serverBootstrap.bind(port).sync();
            System.out.println("server  started in " + port);
            channelFuture.channel().closeFuture().sync();
            System.out.print("server stop ...");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            workGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
            System.out.println("chat server starting in " + port);
        }


    }

    public ChatServer(int port) {
        this.port = port;
    }

    public ChatServer() {
    }

    public static void main(String[] args) {
        int port = ServerConfig.getPort();
        ChatServer chatServer = new ChatServer(port);
        chatServer.start();
    }
}
